﻿
Imports org.model.lib.Model
Imports org.codegen.win.controls
Imports org.model.lib.db
Imports org.model.lib.db.DBUtils
Imports CrystalReportViewer
Imports org.codegen.common.Encryption
Imports org.codegen.common.TranslationServices


Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(ByVal sender As Object, _
                                          ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup

            ModelContext.CurrentUser = New Model.ModelObjectPrincipal(Environment.UserName)
            DBUtils.dbProvider = New ClaimsDBUtilsProvider
            FormsApplicationContext.current.Translator = Translator.getFromConfig
            FormsApplicationContext.current.SaveOnEnterKey = False
            FormsApplicationContext.current.ApplicationDefaultCasing = CharacterCasing.Upper
            FormsApplicationContext.current.ApplicationDefaultFont = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, _
                                System.Drawing.GraphicsUnit.Point, CType(0, Byte))

            FormsApplicationContext.current.ApplicationTitle = "Claims System"

            WinEventLogger.cEventSource = "Claims System"

            If String.IsNullOrEmpty(Me.AKRITASDatabase) Then
                If System.Environment.MachineName.ToLower = "chris-pc" OrElse _
                        System.Environment.MachineName.ToLower = "andreas-pc" Then

                    Me.dbBackupPath = "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Backup\"
                    Me.AIGISDatabase = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=aigisdb;Data Source=.\SQLEXPRESS"
                    Me.AKRITASDatabase = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=akritasdb;Data Source=.\SQLEXPRESS"

                ElseIf System.Environment.MachineName.ToUpper = "XPVM" Then
                    'MsgBox("xpvm")
                    Me.dbBackupPath = "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Backup\"
                    Me.AIGISDatabase = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=aigisdb;Data Source=chris-pc\SQLEXPRESS"
                    Me.AKRITASDatabase = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=akritasdb;Data Source=chris-pc\SQLEXPRESS"

                Else
                    'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLCLAIMS\MSSQL\Backup
                    Me.dbBackupPath = "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLCLAIMS\MSSQL\Backup\"
                    Me.AIGISDatabase = "Password=Cyprus10*;Persist Security Info=True;User ID=sa;Initial Catalog=aigisdb;Data Source=DMS\SQLCLAIMS"
                    Me.AKRITASDatabase = "Password=Cyprus10*;Persist Security Info=True;User ID=sa;Initial Catalog=akritasdb;Data Source=DMS\SQLCLAIMS"
                End If
            End If



            If String.IsNullOrEmpty(CurrentDatabase()) Then
                Dim ds As New DBSelector
                If ds.selectDatabaseFromUI = False Then
                    e.Cancel = True
                    Exit Sub
                End If
            End If

        End Sub
        Public ReadOnly Property CurrentDatabaseDisplay As String
            Get
                If CurrentDatabase = "AIGIS" Then
                    Return "AEGIS"
                Else
                    Return "AKRITAS"
                End If
            End Get
        End Property
        Public Property CurrentDatabase() As String
            Get
                Dim currentdb As String = Microsoft.VisualBasic.GetSetting( _
                    FormsApplicationContext.current.ApplicationTitle, "DB", "CurrentDatabase", String.Empty)
                Return currentdb
            End Get

            Set(ByVal value As String)
                Microsoft.VisualBasic.SaveSetting( _
                          FormsApplicationContext.current.ApplicationTitle, _
                              "DB", "CurrentDatabase", value)
            End Set
        End Property

        
        Public Property initialAllClaimsUpdateDone() As Boolean
            Get
                Dim currentdb As String = Microsoft.VisualBasic.GetSetting( _
                    FormsApplicationContext.current.ApplicationTitle, _
                    "Settings", _
                    CurrentDatabase() & "claimsUpdated", String.Empty)
                Return currentdb = "1"
            End Get

            Set(ByVal value As Boolean)
                Dim setting As String = "1"
                If (Not value) Then
                    setting = "0"
                End If
                Microsoft.VisualBasic.SaveSetting( _
                          FormsApplicationContext.current.ApplicationTitle, _
                              "Settings", CurrentDatabase() & "claimsUpdated", setting)
            End Set
        End Property

        Public Property dbBackupPath() As String
            Get
                Dim tmp As String = Microsoft.VisualBasic.GetSetting( _
                    FormsApplicationContext.current.ApplicationTitle, "DB", "dbBackupPath", String.Empty)
                Return SimpleEncrypt.Decipher(tmp)

            End Get

            Set(ByVal value As String)
                Microsoft.VisualBasic.SaveSetting( _
                          FormsApplicationContext.current.ApplicationTitle, _
                              "DB", "dbBackupPath", SimpleEncrypt.Cipher(value))
            End Set
        End Property

        Public Property AIGISDatabase As String

            Get
                Dim tmp As String = Microsoft.VisualBasic.GetSetting( _
                    FormsApplicationContext.current.ApplicationTitle, "DB", "AIGISDatabase", String.Empty)
                Return SimpleEncrypt.Decipher(tmp)

            End Get

            Set(ByVal value As String)
                Microsoft.VisualBasic.SaveSetting( _
                          FormsApplicationContext.current.ApplicationTitle, _
                              "DB", "AIGISDatabase", SimpleEncrypt.Cipher(value))
            End Set
        End Property

        Public Property AKRITASDatabase As String

            Get
                Dim tmp As String = Microsoft.VisualBasic.GetSetting( _
                    FormsApplicationContext.current.ApplicationTitle, "DB", "AKRITASDatabase", String.Empty)
                Return SimpleEncrypt.Decipher(tmp)

            End Get

            Set(ByVal value As String)
                Microsoft.VisualBasic.SaveSetting( _
                          FormsApplicationContext.current.ApplicationTitle, _
                              "DB", "AKRITASDatabase", SimpleEncrypt.Cipher(value))
            End Set
        End Property

        ''' <summary>
        ''' After the reports are loaded, they are kept in memory using this variable
        ''' </summary>
        ''' <remarks></remarks>
        Private _reportsCache As XMLResourceReportDefinitionsProvider

        Public ReadOnly Property reportsCache As XMLResourceReportDefinitionsProvider
            Get
                If _reportsCache Is Nothing Then
                    _reportsCache = New XMLResourceReportDefinitionsProvider()
                    _reportsCache.setFileOrResourceName("ClaimsUi.ReportDefinitions.xml")

                End If
                Return _reportsCache
            End Get

        End Property

        ''' <summary>
        ''' Log all unhandled exceptions to the windows event log
        ''' </summary>
        Private Sub MyApplication_UnhandledException(ByVal sender As Object, _
                ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) _
                Handles Me.UnhandledException

            MsgBox(e.Exception.Message, MsgBoxStyle.Exclamation, _
                        FormsApplicationContext.current.ApplicationTitle)

            Call WinEventLogger.WriteError(e.Exception)

        End Sub
    End Class



End Namespace

